package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * 增加指定门客门客实力
 */
public class UseAddTargetPatronsQualification extends BaseUseGoods {

	public UseAddTargetPatronsQualification(GamePlayer player, GoodsInfo goodsInfo, long param) {
		super(player, goodsInfo, param);
	}

	@Override
	int checkUse(long count) {
		int patronsId = goodsInfo.getParamList().get(0).intValue();
		UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
		if (null == userPatrons) {
			return GameErrorCode.E_PATRONS_NO_FOUND;
		}
		return 0;
	}

	@Override
	UseGoodsResult executeUse(long count) {
		UseGoodsResult result = new UseGoodsResult();
		int patronsId = goodsInfo.getParamList().get(0).intValue();
		UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
		if (null == userPatrons) {
			result.setRet(GameErrorCode.E_PATRONS_NO_FOUND);
			return result;
		}
		int oldValue = userPatrons.getItemAddQualification();
		Property property = new Property();
		int addQualification = 0;
		for (long i = 0; i < count; i++) {
			int randomValue = GoodsMgr.getAddRandomValue(goodsInfo).intValue();
			addQualification += randomValue;
		}
		property.addProperty(patronsId, addQualification);
		userPatrons.addItemAddQualification(addQualification);

		player.getModule(PatronsModule.class).onPatronsChange(userPatrons);
		player.getModule(PatronsModule.class).syncPatronsData(userPatrons);

		LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), patronsId, addQualification,
				BigInteger.valueOf(oldValue), BigInteger.valueOf(userPatrons.getItemAddQualification()), (int) count);

		result.setReward(PropertyHelper.parsePropertyToString(property));
		return result;
	}
}
